﻿using System;
using System.IO;
using System.Web;
using System.Web.UI;

/// <summary>
/// Summary description for IOLib
/// </summary>

namespace LMD.Lib.IO
{
    public static class IOLib
    {
        /****************************************************************************************
         * NAME     : DownloadFile
         * PURPOSE  : Write a file to response stream
         *
         * REVISIONS
         * No       Date            Version     Author      Description
         * -------  ----------      ----------  ----------  -------------------------------------
         * 1        25/MAR/2010     1.0         IHY         1. Comment Code CREATED
         ****************************************************************************************/
        public static void DownloadFile(String FileFullPathAndName, String DefaultFileName)
        {
            FileInfo file = new FileInfo(FileFullPathAndName);
            if (!file.Exists)
            {
                throw new System.IO.FileNotFoundException("Can’t find that file.");
            }
            else
            {
                if(String.IsNullOrEmpty(DefaultFileName))
                {
                    DefaultFileName = file.Name;
                }

                // clear the current output content from the buffer  
                HttpContext.Current.Response.Clear();

                // add the header that specifies the default filename for the Download/SaveAs dialog  
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + DefaultFileName + "\"");

                // add the header that specifies the file size, so that the browser can show the download progress  
                HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());

                // specify that the requestingPage.Response is a stream that cannot be read by the client and must be downloaded  
                HttpContext.Current.Response.ContentType = "application/octet-stream";

                // send the file stream to the client  
                HttpContext.Current.Response.WriteFile(file.FullName);

                HttpContext.Current.Response.End();
            }
        }

		/****************************************************************************************
         * NAME     : DeleteFile
         * PURPOSE  : Delete a file physically
         *
         * REVISIONS
         * No       Date            Version     Author      Description
         * -------  ----------      ----------  ----------  -------------------------------------
         * 1        21/DEC/2010     1.0         FE          1. DeleteFile Function is Created
         ****************************************************************************************/
        public static void DeleteFile(String FileFullPathAndName)
        {
            //FileInfo TheFile = new FileInfo(MapPath(".") + "\\" + txtFile.Text);
            FileInfo file = new FileInfo(FileFullPathAndName);
            if (!file.Exists)
            {
                throw new System.IO.FileNotFoundException("Can’t find that file.");
            }
            else
            {
                file.Delete();
            }
        }
    }
}